load("@rules_cc//cc:defs.bzl", "cc_test")
load("@rules_fuzzing//fuzzing:cc_defs.bzl", "cc_fuzz_test")
load("//tools:no_undefined.bzl", "cc_library")

package(features = ["layering_check"])

exports_files(
    srcs = ["tox.h"],
    visibility = ["//c-toxcore:__pkg__"],
)

cc_library(
    name = "attributes",
    hdrs = ["attributes.h"],
    visibility = ["//c-toxcore:__subpackages__"],
)

cc_library(
    name = "ccompat",
    srcs = ["ccompat.c"],
    hdrs = ["ccompat.h"],
    visibility = ["//c-toxcore:__subpackages__"],
    deps = [":attributes"],
)

cc_library(
    name = "util",
    srcs = ["util.c"],
    hdrs = ["util.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
        "//c-toxcore/testing/fuzzing:__pkg__",
        "//c-toxcore/toxav:__pkg__",
    ],
    deps = [
        ":attributes",
        ":ccompat",
        "@pthread",
    ],
)

cc_test(
    name = "util_test",
    size = "small",
    srcs = ["util_test.cc"],
    deps = [
        ":crypto_core",
        ":util",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "bin_pack",
    srcs = ["bin_pack.c"],
    hdrs = ["bin_pack.h"],
    visibility = ["//c-toxcore:__subpackages__"],
    deps = [
        ":attributes",
        ":ccompat",
        "//c-toxcore/third_party:cmp",
    ],
)

cc_library(
    name = "bin_unpack",
    srcs = ["bin_unpack.c"],
    hdrs = ["bin_unpack.h"],
    visibility = ["//c-toxcore:__subpackages__"],
    deps = [
        ":attributes",
        ":ccompat",
        "//c-toxcore/third_party:cmp",
    ],
)

cc_test(
    name = "bin_pack_test",
    size = "small",
    srcs = ["bin_pack_test.cc"],
    deps = [
        ":bin_pack",
        ":bin_unpack",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "crypto_core",
    srcs = ["crypto_core.c"],
    hdrs = ["crypto_core.h"],
    visibility = ["//c-toxcore:__subpackages__"],
    deps = [
        ":attributes",
        ":ccompat",
        "@libsodium",
    ],
)

cc_test(
    name = "crypto_core_test",
    size = "small",
    srcs = ["crypto_core_test.cc"],
    flaky = True,
    deps = [
        ":crypto_core",
        ":util",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "list",
    srcs = ["list.c"],
    hdrs = ["list.h"],
    deps = [
        ":attributes",
        ":ccompat",
    ],
)

cc_test(
    name = "list_test",
    size = "small",
    srcs = ["list_test.cc"],
    deps = [
        ":list",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "logger",
    srcs = ["logger.c"],
    hdrs = ["logger.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
        "//c-toxcore/toxav:__pkg__",
    ],
    deps = [
        ":attributes",
        ":ccompat",
    ],
)

cc_library(
    name = "state",
    srcs = ["state.c"],
    hdrs = ["state.h"],
    deps = [
        ":ccompat",
        ":logger",
    ],
)

cc_library(
    name = "mono_time",
    srcs = ["mono_time.c"],
    hdrs = ["mono_time.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
        "//c-toxcore/testing:__pkg__",
        "//c-toxcore/toxav:__pkg__",
    ],
    deps = [
        ":attributes",
        ":ccompat",
        "@pthread",
    ],
)

cc_test(
    name = "mono_time_test",
    size = "small",
    srcs = ["mono_time_test.cc"],
    deps = [
        ":mono_time",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "shared_key_cache",
    srcs = ["shared_key_cache.c"],
    hdrs = ["shared_key_cache.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
        "//c-toxcore/testing:__pkg__",
        "//c-toxcore/toxav:__pkg__",
    ],
    deps = [
        ":ccompat",
        ":crypto_core",
        ":mono_time",
    ],
)

cc_library(
    name = "network",
    srcs = ["network.c"],
    hdrs = ["network.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
        "//c-toxcore/testing/fuzzing:__pkg__",
        "//c-toxcore/toxav:__pkg__",
    ],
    deps = [
        ":ccompat",
        ":crypto_core",
        ":logger",
        ":mono_time",
        ":util",
        "@libsodium",
        "@psocket",
        "@pthread",
    ],
)

cc_test(
    name = "network_test",
    size = "small",
    srcs = ["network_test.cc"],
    deps = [
        ":network",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "timed_auth",
    srcs = ["timed_auth.c"],
    hdrs = ["timed_auth.h"],
    deps = [
        ":ccompat",
        ":crypto_core",
        ":mono_time",
    ],
)

cc_library(
    name = "ping_array",
    srcs = ["ping_array.c"],
    hdrs = ["ping_array.h"],
    deps = [
        ":ccompat",
        ":crypto_core",
        ":mono_time",
        ":util",
    ],
)

cc_test(
    name = "ping_array_test",
    size = "small",
    srcs = ["ping_array_test.cc"],
    deps = [
        ":mono_time",
        ":ping_array",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "LAN_discovery",
    srcs = ["LAN_discovery.c"],
    hdrs = ["LAN_discovery.h"],
    visibility = [
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
        "//c-toxcore/testing:__pkg__",
    ],
    deps = [
        ":ccompat",
        ":crypto_core",
        ":network",
        ":util",
    ],
)

cc_library(
    name = "DHT",
    srcs = [
        "DHT.c",
        "ping.c",
    ],
    hdrs = [
        "DHT.h",
        "ping.h",
    ],
    visibility = [
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
        "//c-toxcore/testing:__pkg__",
    ],
    deps = [
        ":LAN_discovery",
        ":attributes",
        ":ccompat",
        ":crypto_core",
        ":logger",
        ":mono_time",
        ":network",
        ":ping_array",
        ":shared_key_cache",
        ":state",
        ":util",
    ],
)

cc_test(
    name = "DHT_test",
    size = "small",
    srcs = ["DHT_test.cc"],
    deps = [
        ":DHT",
        ":crypto_core",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_fuzz_test(
    name = "DHT_fuzz_test",
    srcs = ["DHT_fuzz_test.cc"],
    corpus = ["//tools/toktok-fuzzer/corpus:DHT_fuzz_test"],
    deps = [
        ":DHT",
        "//c-toxcore/testing/fuzzing:fuzz_support",
    ],
)

cc_library(
    name = "onion",
    srcs = ["onion.c"],
    hdrs = ["onion.h"],
    visibility = ["//c-toxcore/auto_tests:__pkg__"],
    deps = [
        ":DHT",
        ":ccompat",
        ":crypto_core",
        ":logger",
        ":mono_time",
        ":shared_key_cache",
        ":util",
    ],
)

cc_library(
    name = "forwarding",
    srcs = ["forwarding.c"],
    hdrs = ["forwarding.h"],
    visibility = ["//c-toxcore/auto_tests:__pkg__"],
    deps = [
        ":DHT",
        ":ccompat",
        ":network",
        ":timed_auth",
    ],
)

cc_fuzz_test(
    name = "forwarding_fuzz_test",
    srcs = ["forwarding_fuzz_test.cc"],
    #corpus = ["//tools/toktok-fuzzer/corpus:forwarding_fuzz_test"],
    deps = [
        ":forwarding",
        "//c-toxcore/testing/fuzzing:fuzz_support",
        "//c-toxcore/testing/fuzzing:fuzz_tox",
    ],
)

cc_library(
    name = "announce",
    srcs = ["announce.c"],
    hdrs = ["announce.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
    ],
    deps = [
        ":LAN_discovery",
        ":ccompat",
        ":forwarding",
        ":shared_key_cache",
        ":timed_auth",
        ":util",
    ],
)

cc_library(
    name = "TCP_common",
    srcs = ["TCP_common.c"],
    hdrs = ["TCP_common.h"],
    visibility = ["//c-toxcore/auto_tests:__pkg__"],
    deps = [
        ":attributes",
        ":ccompat",
        ":crypto_core",
        ":network",
    ],
)

cc_library(
    name = "TCP_server",
    srcs = ["TCP_server.c"],
    hdrs = ["TCP_server.h"],
    copts = select({
        "//tools/config:linux": ["-DTCP_SERVER_USE_EPOLL=1"],
        "//conditions:default": [],
    }),
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
    ],
    deps = [
        ":TCP_common",
        ":ccompat",
        ":crypto_core",
        ":forwarding",
        ":list",
        ":mono_time",
        ":onion",
        ":util",
    ],
)

cc_library(
    name = "TCP_client",
    srcs = ["TCP_client.c"],
    hdrs = ["TCP_client.h"],
    visibility = ["//c-toxcore/auto_tests:__pkg__"],
    deps = [
        ":TCP_common",
        ":ccompat",
        ":crypto_core",
        ":forwarding",
        ":mono_time",
        ":network",
        ":util",
    ],
)

cc_library(
    name = "TCP_connection",
    srcs = ["TCP_connection.c"],
    hdrs = ["TCP_connection.h"],
    visibility = ["//c-toxcore/auto_tests:__pkg__"],
    deps = [
        ":DHT",
        ":TCP_client",
        ":TCP_common",
        ":ccompat",
        ":crypto_core",
        ":list",
        ":mono_time",
        ":onion",
        ":util",
    ],
)

cc_test(
    name = "TCP_connection_test",
    size = "small",
    srcs = ["TCP_connection_test.cc"],
    deps = [
        ":TCP_connection",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "net_crypto",
    srcs = ["net_crypto.c"],
    hdrs = ["net_crypto.h"],
    visibility = ["//c-toxcore/auto_tests:__pkg__"],
    deps = [
        ":DHT",
        ":LAN_discovery",
        ":TCP_connection",
        ":ccompat",
        ":list",
        ":logger",
        ":mono_time",
        ":util",
    ],
)

cc_library(
    name = "onion_announce",
    srcs = ["onion_announce.c"],
    hdrs = ["onion_announce.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
    ],
    deps = [
        ":DHT",
        ":LAN_discovery",
        ":ccompat",
        ":logger",
        ":mono_time",
        ":onion",
        ":shared_key_cache",
        ":timed_auth",
        ":util",
    ],
)

cc_library(
    name = "group_announce",
    srcs = ["group_announce.c"],
    hdrs = ["group_announce.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
    ],
    deps = [
        ":DHT",
        ":LAN_discovery",
        ":ccompat",
        ":mono_time",
        ":util",
    ],
)

cc_test(
    name = "group_announce_test",
    size = "small",
    srcs = ["group_announce_test.cc"],
    deps = [
        ":group_announce",
        ":mono_time",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "group_onion_announce",
    srcs = ["group_onion_announce.c"],
    hdrs = ["group_onion_announce.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/other/bootstrap_daemon:__pkg__",
    ],
    deps = [
        ":ccompat",
        ":group_announce",
        ":onion_announce",
    ],
)

cc_fuzz_test(
    name = "group_announce_fuzz_test",
    srcs = ["group_announce_fuzz_test.cc"],
    #corpus = ["//tools/toktok-fuzzer/corpus:group_announce_fuzz_test"],
    deps = [
        ":group_announce",
        "//c-toxcore/testing/fuzzing:fuzz_support",
    ],
)

cc_library(
    name = "onion_client",
    srcs = ["onion_client.c"],
    hdrs = ["onion_client.h"],
    visibility = ["//c-toxcore/auto_tests:__pkg__"],
    deps = [
        ":DHT",
        ":LAN_discovery",
        ":ccompat",
        ":group_onion_announce",
        ":mono_time",
        ":net_crypto",
        ":network",
        ":onion_announce",
        ":ping_array",
        ":util",
    ],
)

cc_library(
    name = "friend_connection",
    srcs = ["friend_connection.c"],
    hdrs = ["friend_connection.h"],
    visibility = ["//c-toxcore/auto_tests:__pkg__"],
    deps = [
        ":DHT",
        ":LAN_discovery",
        ":ccompat",
        ":mono_time",
        ":net_crypto",
        ":onion_client",
        ":util",
    ],
)

cc_test(
    name = "friend_connection_test",
    size = "small",
    srcs = ["friend_connection_test.cc"],
    deps = [
        ":friend_connection",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "friend_requests",
    srcs = ["friend_requests.c"],
    hdrs = ["friend_requests.h"],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/testing:__pkg__",
    ],
    deps = [
        ":ccompat",
        ":friend_connection",
        ":network",
        ":util",
    ],
)

cc_library(
    name = "group_moderation",
    srcs = ["group_moderation.c"],
    hdrs = ["group_moderation.h"],
    deps = [
        ":DHT",
        ":ccompat",
        ":crypto_core",
        ":logger",
        ":mono_time",
        ":network",
        ":util",
        "@libsodium",
    ],
)

cc_test(
    name = "group_moderation_test",
    size = "small",
    srcs = ["group_moderation_test.cc"],
    deps = [
        ":crypto_core",
        ":group_moderation",
        ":logger",
        ":util",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_fuzz_test(
    name = "group_moderation_fuzz_test",
    srcs = ["group_moderation_fuzz_test.cc"],
    corpus = ["//tools/toktok-fuzzer/corpus:group_moderation_fuzz_test"],
    deps = [
        ":group_moderation",
        "//c-toxcore/testing/fuzzing:fuzz_support",
    ],
)

cc_library(
    name = "Messenger",
    srcs = [
        "Messenger.c",
        "group_chats.c",
        "group_connection.c",
        "group_pack.c",
    ],
    hdrs = [
        "Messenger.h",
        "group_chats.h",
        "group_common.h",
        "group_connection.h",
        "group_pack.h",
    ],
    visibility = [
        "//c-toxcore/auto_tests:__pkg__",
        "//c-toxcore/other:__pkg__",
        "//c-toxcore/testing:__pkg__",
        "//c-toxcore/toxav:__pkg__",
    ],
    deps = [
        ":DHT",
        ":LAN_discovery",
        ":TCP_connection",
        ":TCP_server",
        ":announce",
        ":bin_pack",
        ":bin_unpack",
        ":ccompat",
        ":crypto_core",
        ":forwarding",
        ":friend_connection",
        ":friend_requests",
        ":group_announce",
        ":group_moderation",
        ":group_onion_announce",
        ":logger",
        ":mono_time",
        ":net_crypto",
        ":network",
        ":onion_announce",
        ":state",
        ":util",
        "@libsodium",
    ],
)

cc_library(
    name = "group",
    srcs = ["group.c"],
    hdrs = ["group.h"],
    visibility = ["//c-toxcore/toxav:__pkg__"],
    deps = [
        ":Messenger",
        ":ccompat",
        ":mono_time",
        ":state",
        ":util",
    ],
)

cc_library(
    name = "tox",
    srcs = [
        "tox.c",
        "tox_api.c",
        "tox_private.c",
    ],
    hdrs = [
        "tox.h",
        "tox_private.h",
        "tox_struct.h",
    ],
    visibility = ["//c-toxcore:__subpackages__"],
    deps = [
        ":Messenger",
        ":ccompat",
        ":group",
        ":group_moderation",
        ":logger",
        ":mono_time",
        ":network",
        "//c-toxcore/toxencryptsave:defines",
    ],
)

cc_test(
    name = "tox_test",
    size = "small",
    srcs = ["tox_test.cc"],
    deps = [
        ":crypto_core",
        ":tox",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "tox_unpack",
    srcs = ["tox_unpack.c"],
    hdrs = ["tox_unpack.h"],
    visibility = ["//c-toxcore:__subpackages__"],
    deps = [
        ":attributes",
        ":bin_unpack",
        ":ccompat",
        ":tox",
    ],
)

cc_library(
    name = "tox_events",
    srcs = ["tox_events.c"] + glob([
        "events/*.c",
        "events/*.h",
    ]),
    hdrs = ["tox_events.h"],
    visibility = ["//c-toxcore:__subpackages__"],
    deps = [
        ":attributes",
        ":bin_pack",
        ":bin_unpack",
        ":ccompat",
        ":tox",
        ":tox_unpack",
        "//c-toxcore/third_party:cmp",
    ],
)

cc_test(
    name = "tox_events_test",
    size = "small",
    srcs = ["tox_events_test.cc"],
    deps = [
        ":crypto_core",
        ":tox_events",
        "@com_google_googletest//:gtest",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_fuzz_test(
    name = "tox_events_fuzz_test",
    srcs = ["tox_events_fuzz_test.cc"],
    corpus = ["//tools/toktok-fuzzer/corpus:tox_events_fuzz_test"],
    deps = [":tox_events"],
)

cc_library(
    name = "tox_dispatch",
    srcs = ["tox_dispatch.c"],
    hdrs = ["tox_dispatch.h"],
    visibility = ["//c-toxcore:__subpackages__"],
    deps = [
        ":ccompat",
        ":tox_events",
    ],
)

alias(
    name = "toxcore",
    actual = ":tox_dispatch",
    visibility = ["//c-toxcore:__subpackages__"],
)

sh_library(
    name = "cimple_files",
    srcs = glob([
        "events/*.c",
        "events/*.h",
        "*.c",
        "*.h",
    ]),
    visibility = ["//c-toxcore/testing:__pkg__"],
)
